<template>
  <div>
    <h1>公园信息管理</h1>
    <el-divider />
  </div>
  <div>
    <el-form :model="search" ref="form" :inline="false" size="default" class="flex justify-between">
      <el-form-item label="">
        <i class="mr-5">关键字</i>
        <el-input v-model="search.name" placeholder="请输入关键字" class="ipt" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch" size="large">搜索</el-button>
        <el-button @click="handleDel" size="large">重置</el-button>
      </el-form-item>
      <div>
        <ul class="flex m1">
          <li>
            <h1>{{ parks.totals }}</h1>
            <p>公园总数</p>
          </li>
          <li>
            <h1>{{ areas }}</h1>
            <p>公园总面积</p>
          </li>
          <li>
            <h1>{{ visitors }}</h1>
            <p>客容量面积</p>
          </li>
          <li>
            <h1>{{ num }}</h1>
            <p>车位量</p>
          </li>
        </ul>
      </div>
    </el-form>
    <div class="flex justify-between mb-4">
      <div>
        批量删除
        <el-button :icon="Delete" circle type="danger" class="ml-2" @click="handleMuchDel" />
      </div>
      <el-button type="primary" size="default" @click="handleDrawer(null)">新增</el-button>
    </div>
  </div>
  <el-table :data="parkData" border stripe style="width: 100%" height="260" @selection-change="handleChange"
    :header-cell-style="headerCellStyle">
    <el-table-column type="selection" width="80px" />
    <el-table-column label="序号" type="index" width="80px" />
    <el-table-column label="公园名称" prop="name" width="130px" />
    <el-table-column label="公园地址" prop="address" width="130px" />
    <el-table-column label="公园面积(亩)" prop="area" width="130px" />
    <el-table-column label="公园类型" prop="type" width="130px" />
    <el-table-column label="园区级别" prop="rank" width="130px" />
    <el-table-column label="开放时间" prop="time" width="200px" />
    <el-table-column label="最大客容量（人次）" prop="visitor" width="130px" />
    <el-table-column label="车位数" prop="number" width="130px" />
    <el-table-column label="公园简介" prop="intro" width="130px" />
    <el-table-column label="操作" width="150px" fixed="right">
      <template #default="scope">
        <div>
          <el-button type="primary" size="small" @click="handleDrawer(scope.row)">编辑</el-button>
          <el-popconfirm title="是否确定删除？" confirmButtonText="确认" cancelButtonText="取消" icon="WarningFilled"
            iconColor="#f90" @confirm="handleDele(scope.row, scope.$index)">
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <div class="flex justify-end mt-10">
    <el-pagination v-model:current-page="current" v-model:page-size="pageSize" :page-sizes="[15, 50, 100]" :small="false"
      :disabled="false" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="parks.totals"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>

  <el-drawer :title="rowData ? '编辑公园信息' : '新增公园信息'" v-model="drawer" direction="rtl" size="50%" :destroy-on-close="true"
    :show-close="true">
    <Add :edata="rowData" @update:table-data="handleUpdate" />
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { Delete } from '@element-plus/icons-vue'
import { Overview, parkDel } from '../../api/Park/park'
import Add from './Components/add.vue'
import { usePark } from '../../store/modules/park'

const parks = usePark()

const search = reactive({
  name: ''
})

let parkDatas = ref<any>([])
const num = ref(0)
const areas = ref(0)
const visitors = ref(0)
onMounted(() => {
  Overview().then((res) => {
    console.log('公园信息', res.data.results)
    parkDatas.value = res.data.results
    parkDatas.value.forEach((e) => {
      num.value += e.number
      areas.value += e.area
      visitors.value += e.visitor
      // console.log(e.visitor, '2222')
    })
  })
})

const drawer = ref<boolean>(false)
// 搜索
const handleSearch = async () => {
  console.log(search)
  current.value = 1 //重新回到第一页
  let res = await Overview(1, pageSize.value, search)
  let { results, count } = res.data
  parkData.value = results
  total.value = count
  parks.totals = total.value
}

// 重置
const handleDel = () => {
  search.name = ''
  parkData.value = res.data.results
}
interface parkDataType {
  address: string
  name: string
  area: number
  type: string
  number: number
  rank: string
  time: string
  visitor: number
  intro: string
  objectId?: string
  lng: number
  lat: number
}
const parkData = ref<Array<parkDataType | null>>([])
const total = ref<number>(0)
let res = await Overview()
// console.log(res);

let { results, count } = res.data
parkData.value = results
total.value = count
parks.totals = total.value

// 编辑
const rowData = ref<parkDataType | null>(null) //存放新增、编辑、查看的数据包
const handleDrawer = (row: parkDataType | null) => {
  console.log('区分新增、编辑', row)
  drawer.value = true
  rowData.value = row
}

const handleUpdate = (newpark: parkDataType) => {
  drawer.value = false
  if (rowData.value) {
    // 编辑后,更新本地列表
    for (let i = 0; i < parkData.value.length; i++) {
      if (parkData.value[i]!.objectId == newpark.objectId) {
        parkData.value[i] = newpark
        return
      }
    }
  } else {
    parks.totals++
    // 新增后,更新本地列表
    // console.log('123', newhealth);
    parkData.value.push(newpark)
  }
}
// 批量删除
const mutiple = ref([]) //定义空数组，临时存放数据
const handleChange = (val) => {
  console.log(val, 'val')
  mutiple.value = val
  // console.log('1111111', mutiple.value)
}
const handleMuchDel = () => {
  for (let i = mutiple.value.length - 1; i >= 0; i--) {
    const idx = parkData.value.indexOf(mutiple.value[i]) //获取表里的下标
    handleDele(mutiple.value[i], idx)
  }
}

// 删除
const handleDele = (row: parkDataType, idx: number) => {
  console.log('删除', row, idx)
  parkDel(row.objectId!).then(async () => {
    parkData.value.splice(idx, 1) //删除本地
    // parkData.value = res.data.results
    let res = await Overview(current.value, pageSize.value, search)
    let { results, count } = res.data
    parkData.value = results
    total.value = count
    parks.totals = total.value
    console.log(res)
  })
}

// 分页
const current = ref<number>(1)
const pageSize = ref<number>(10) //每页显示的数据条数
// 页码变化触发事件
const handleCurrentChange = async (page: number) => {
  console.log(page)
  let res = await Overview(page, pageSize.value, search)
  parkData.value = res.data.results
}
// 每页数量变化事件
const handleSizeChange = async () => {
  current.value = 1 //重新回到第一页
  let res = await Overview(1, pageSize.value, search)
  parkData.value = res.data.results
}

const headerCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex || columnIndex === 0) {
    return {
      backgroundColor: '#f3f4f6'
    }
  }
  console.log(row, column, rowIndex)
}
</script>

<style scoped>
.ipt {
  width: 300px;
  height: 50px;
}

li {
  width: 100px;
  height: 100px;
  background-color: #facd91;
  margin-right: 5px;
  text-align: center;
  line-height: 40px;
}
</style>
